/*
 * Geometry.java
 *
 * Created on 27 December 2007, 12:06
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package hextd.util;

import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;

/**
 *
 * @author hylke
 */
public class Geometry {
	/* top-middle
	 * top-right
	 * bottom-right
	 * bottom-middle
	 * bottom-left
	 * top-left
	 */

	public static Ellipse2D createCircle(double x, double y, double r) {
		return new Ellipse2D.Double(x - r, y - r, r * 2, r * 2);
	}

	public static double[][] createHexCoords(double scale, double x, double y, boolean leftRight) {
		double width = Math.sin(Math.PI / 3.0) * scale;

		if (leftRight) {
			double[][] points = {
				{x + 0, y - scale},
				{x + width, y - scale / 2},
				{x + width, y + scale / 2},
				{x + 0, y + scale},
				{x - width, y + scale / 2},
				{x - width, y - scale / 2}
			};
			return points;
		} else {
			double[][] points = {
				{x + scale, y + 0},
				{x + scale / 2, y + width},
				{x - scale / 2, y + width},
				{x - scale, y - 0},
				{x - scale / 2, y - width},
				{x + scale / 2, y - width}
			};
			return points;
		}
	}

	public static double[][] createHexCoords(double scale, double x, double y) {
		return createHexCoords(scale, x, y, true);
	}

	public static double[][] createHexCoords(double scale) {
		return createHexCoords(scale, 0, 0);
	}

	public static GeneralPath coordsToGeneralPath(double[][] points, boolean closed) {
		GeneralPath poly = new GeneralPath(GeneralPath.WIND_EVEN_ODD, points.length);
		poly.moveTo(points[0][0], points[0][1]);
		for (int index = 1; index < points.length; index++) {
			poly.lineTo(points[index][0], points[index][1]);
		}
		if (closed) {
			poly.closePath();
		}
		return poly;
	}
}
